<?php

/**
 * ECSHOP 夺宝奇兵前台页面
 * ============================================================================
 * 版权所有 2005-2010 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liuhui $
 * $Id: snatch.php 17063 2010-03-25 06:35:46Z liuhui $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');


/* 显示页面部分 */

$taobao_packages = get_package_list("taobao");
$paipai_packages = get_package_list("paipai");

 assign_template();
$smarty->assign('page_title', "达达66数据包下载");    // 页面标题

$smarty->assign('taobao_packages',  $taobao_packages);
$smarty->assign('taobao_packages_count',  count($taobao_packages));
$smarty->assign('paipai_packages',  $paipai_packages);
$smarty->assign('paipai_packages_count',  count($paipai_packages));
$smarty->assign('feed_url',         ($_CFG['rewrite'] == 1) ? "feed-typesnatch.xml" : 'feed.php?type=snatch'); // RSS URL
$smarty->display('data_package.dwt');

exit;



/**
 * 取的最近的几次活动。
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_package_list($type)
{
    $now = gmtime();
    if($_REQUEST['zhejuan_supplyinfo'] && false)
    {
    $sql = 'SELECT * '.
           ' FROM ' . $GLOBALS['ecs']->table('data_package').
           " WHERE `type`='" . $type ."' AND `suppliers_id`=".$_REQUEST['zhejuan_supplyinfo']['suppliers_id'].
           " ORDER BY pai_xu DESC";
    }else{
    	$sql = 'SELECT * '.
           ' FROM ' . $GLOBALS['ecs']->table('data_package').
           " WHERE `type`='" . $type ."'".
           " ORDER BY pai_xu DESC";
    }
    $package_list = array();
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->FetchRow($res))
    {
        $package_list[] = array(
            'name' => $row['name'],
            'file' => $row['file'],
            'timestamp' => $row['timestamp'],
            'url'=>"data_package/".$row['file']
                            );
    }
    return $package_list;

}

/**
 * 取得当前活动信息
 *
 * @access  public
 *
 * @return 活动名称
 */
function get_snatch($id)
{
    $sql = "SELECT g.goods_id, g.goods_sn, g.is_real, g.goods_name, g.extension_code, g.market_price, g.shop_price AS org_price, product_id, " .
                    "IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, " .
                    "g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb, " .
                    "ga.act_name AS snatch_name, ga.start_time, ga.end_time, ga.ext_info, ga.act_desc AS `desc` ".
                "FROM " .$GLOBALS['ecs']->table('goods_activity'). " AS ga " .
                "LEFT JOIN " . $GLOBALS['ecs']->table('goods')." AS g " .
                    "ON g.goods_id = ga.goods_id " .
                "LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp " .
                    "ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' " .
                "WHERE ga.act_id = '$id' AND g.is_delete = 0";

    $goods = $GLOBALS['db']->GetRow($sql);

    if ($goods)
    {
        $promote_price          = bargain_price($goods['promote_price'], $goods['promote_start_date'], $goods['promote_end_date']);
        $goods['formated_market_price']  = price_format($goods['market_price']);
        $goods['formated_shop_price']    = price_format($goods['shop_price']);
        $goods['formated_promote_price'] = ($promote_price > 0) ? price_format($promote_price) : '';
        $goods['goods_thumb']   = get_image_path($goods['goods_id'], $goods['goods_thumb'], true);
        $goods['url']           = build_uri('goods', array('gid'=>$goods['goods_id']), $goods['goods_name']);
        $goods['start_time']    = local_date($GLOBALS['_CFG']['time_format'], $goods['start_time']);

        $info = unserialize($goods['ext_info']);
        if ($info)
        {
            foreach ($info as $key => $val)
            {
                $goods[$key] = $val;
            }
            $goods['is_end'] = gmtime() > $goods['end_time'];
            $goods['formated_start_price'] = price_format($goods['start_price']);
            $goods['formated_end_price'] = price_format($goods['end_price']);
            $goods['formated_max_price'] = price_format($goods['max_price']);
        }
        /* 将结束日期格式化为格林威治标准时间时间戳 */
        $goods['gmt_end_time']  = $goods['end_time'];
        $goods['end_time']      = local_date($GLOBALS['_CFG']['time_format'], $goods['end_time']);
        $goods['snatch_time']   = sprintf($GLOBALS['_LANG']['snatch_start_time'], $goods['start_time'], $goods['end_time']);

        return $goods;
    }
    else
    {
        return false;
    }
}

/**
 * 获取最近要到期的活动id，没有则返回 0
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_last_snatch()
{
    $now = gmtime();
    $sql = 'SELECT act_id FROM ' . $GLOBALS['ecs']->table('goods_activity').
           " WHERE  start_time < '$now' AND end_time > '$now' AND act_type = " . GAT_SNATCH .
           " ORDER BY end_time ASC LIMIT 1";
    return $GLOBALS['db']->GetOne($sql);
}

?>